package org.enwey.icarenew.Component;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.API.RequestResult;
import org.enwey.icarenew.APPUtils.StringUtil;
import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.Login.PerfectUserInfo;
import org.enwey.icarenew.Activity.Personal.AttentionActivity;
import org.enwey.icarenew.Activity.Personal.FansActivity;
import org.enwey.icarenew.Activity.Personal.ShareNewActivity;
import org.enwey.icarenew.Activity.Personal.SubscribeActivity;
import org.enwey.icarenew.Activity.Setting.SettingActivity;
import org.enwey.icarenew.Manager.SettingsManager;
import org.enwey.icarenew.Model.User;
import org.enwey.icarenew.R;

import java.util.Hashtable;

/**
 * Created by John on 1/26/2015.
 */
public class UserNewHeaderView extends LinearLayout {
    private LinearLayout mSupport;
    private CircleImageView mUserSetting;
    private ImageView mThumb;
    private TextView mSupportSum;
    private LinearLayout mShare;
    private TextView mSubscribe;
    private TextView mAttention;
    private TextView mFans;
    private TextView mNick;
    private TextView mSummary;
    private int num;
    private User mLoginUser;
    private Context mContext;

    public UserNewHeaderView(Context context) {
        this(context, null, 0);
    }

    public UserNewHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UserNewHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.user_new_header, this);
        mContext = getContext();
        mLoginUser = SettingsManager.getLoginUser();
        findView(view);
        setHeaderImage(mLoginUser);
        registerListener();
        setUserData(mLoginUser);
    }

    private void findView(View view) {
        mUserSetting = (CircleImageView) view.findViewById(R.id.user_new_header_img);
        mSupport = (LinearLayout) view.findViewById(R.id.user_new_header_support);
        mThumb = (ImageView) view.findViewById(R.id.user_new_header_support_img);
        mSupportSum = (TextView) view.findViewById(R.id.user_new_header_support_sum);
        mShare = (LinearLayout) view.findViewById(R.id.user_new_header_share_btn);
        mSubscribe = (TextView) view.findViewById(R.id.user_new_header_subscribe);
        mAttention = (TextView) view.findViewById(R.id.user_new_header_attention);
        mFans = (TextView) view.findViewById(R.id.user_new_header_fans);
        mNick = (TextView) view.findViewById(R.id.user_new_header_name);
        mSummary = (TextView) view.findViewById(R.id.user_new_header_summary);
    }

    private void registerListener() {
        OnClickListener register = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                if (v.getId() == R.id.user_new_header_support) {
                    num = Integer.parseInt(mLoginUser.support);
                    doSupport();
                    return;
                } else if (v.getId() == mUserSetting.getId()) {
                    intent.setClass(mContext, PerfectUserInfo.class);
                    intent.putExtra("userId", "需要改成用户ID");
                } else if (v == mThumb) {

                } else if (v == mShare) {
                    intent.setClass(mContext, ShareNewActivity.class);
                } else if (v == mSubscribe) {
                    intent.setClass(mContext, SubscribeActivity.class);
                    intent.putExtra("userId", "需要改成用户ID");

                } else if (v == mAttention) {
                    intent.setClass(mContext, AttentionActivity.class);
                    intent.putExtra("userId", "需要改成用户ID");

                } else if (v == mFans) {
                    intent.setClass(mContext, FansActivity.class);
                    intent.putExtra("userId", "需要改成用户ID");

                }

                mContext.startActivity(intent);
            }
        };
        mSupport.setOnClickListener(register);
        mShare.setOnClickListener(register);
        mSubscribe.setOnClickListener(register);
        mAttention.setOnClickListener(register);
        mFans.setOnClickListener(register);
        mUserSetting.setOnClickListener(register);
    }

    private void doSupport() {
        num += 1;
        CallAPIDoSupport();
    }

    public void setHeaderImage(User user) {
        mUserSetting.setErrorImageResId(R.drawable.img_header_big);
        mUserSetting.setDefaultImageResId(R.drawable.img_header_big);

        mUserSetting.setImageUrl(APIManager.toAbsoluteUrl(StringUtil.showMessage(user.avatar)),
                APIManager.getInstance(getContext()).getImageLoader());


    }

    private void CallAPIDoSupport() {
        Hashtable<String, String> parameters = new Hashtable<>();
        parameters.put("account", mLoginUser.account);
        parameters.put("support", num + "");
        APIManager.getInstance(getContext()).doSupport(parameters, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ToastUtil.show(getContext(), "oh!点赞失败");
            }
        }, new Response.Listener<RequestResult<User>>() {
            @Override
            public void onResponse(RequestResult<User> response) {
                if (response.code != 200) {
                    ToastUtil.show(getContext(), "oh!点赞失败");
                    return;
                }
                if (response != null) {
                    mLoginUser.support = num + "";
                    mSupportSum.setText(num + "");
                    SettingsManager.deleteLoginUser();
                    SettingsManager.saveLoginUser(mLoginUser);
                }
            }
        });
    }

    public void setUserData(User user) {
        if (user.nick != null) {
            mNick.setText(user.nick + "");
        }
        if (user.summary != null) {
            mSummary.setText(user.summary + "");
        }
        if (user.support != null) {
            mSupportSum.setText(user.support + "");
            num = Integer.parseInt(user.support);
        }
        if (user.subscribe != null) {
            mSubscribe.setText(user.subscribe + "");
        }
        if (user.attention != null) {
            mAttention.setText(user.attention + "");
        }
        if (user.fans != null) {
            mFans.setText(user.fans + "");
        }
    }
}
